package com.lsw.jgyf.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lsw.jgyf.R;
import com.lsw.shareUser.ShareUserInfoUtil;
import com.lsw.util.DeviceUtil;
import com.lsw.util.Helper;
import com.lsw.util.StatusBarCompat;


/**
 * 引导页
 *
 * @author lsw
 */
public class GuiderActivity extends Activity implements View.OnClickListener {
    private Context context;
    ViewPager viewpager;
    private TextView tv_jinru;
    private TextView bt_tiaoguo;
    private int[] images = {R.mipmap.yindaoye1, R.mipmap.yindaoye2, R.mipmap.yindaoye3};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        SetTranslanteBar();
        super.onCreate(savedInstanceState);
        context = this;
        Helper.statusBarLightMode(this);
        setContentView(R.layout.activity_guide);
        initViews();
        initData();
    }

    /**
     * 沉浸状态栏（4.4以上系统有效）
     */
    protected void SetTranslanteBar() {
        StatusBarCompat.translucentStatusBar(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_tiaoguo:
                Intent intent1 = new Intent(context, MainActivity.class);
                startActivity(intent1);
                finish();
                break;
            case R.id.tv_jinru:
                Intent intent = new Intent(context, LoginActivity.class);
                startActivity(intent);
                finish();
                break;
        }
    }

    class GuideAdapter extends PagerAdapter {
        public GuideAdapter() {
        }

        @Override
        public int getCount() {
            return images.length;
        }

        @Override
        public View instantiateItem(ViewGroup container, final int position) {
            RelativeLayout relativeLayout = new RelativeLayout(
                    container.getContext());
            int res = images[position];
            ImageView photoView = new ImageView(container.getContext());
            photoView.setBackgroundColor(Color.WHITE);
            photoView.setScaleType(ScaleType.FIT_XY);
            photoView.setImageResource(res);
            relativeLayout.addView(photoView, LayoutParams.MATCH_PARENT,
                    LayoutParams.MATCH_PARENT);
            container.addView(relativeLayout, LayoutParams.MATCH_PARENT,
                    LayoutParams.MATCH_PARENT);
            return relativeLayout;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

    }

    protected void initViews() {
        viewpager = findViewById(R.id.viewpager);
        tv_jinru = findViewById(R.id.tv_jinru);
        bt_tiaoguo = findViewById(R.id.bt_tiaoguo);
        tv_jinru.setOnClickListener(this);
        bt_tiaoguo.setOnClickListener(this);
    }

    protected void initData() {
        viewpager.setAdapter(new GuideAdapter());
        viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        tv_jinru.setVisibility(View.GONE);
                        bt_tiaoguo.setVisibility(View.INVISIBLE);
                        break;
                    case 1:
                        tv_jinru.setVisibility(View.GONE);
                        bt_tiaoguo.setVisibility(View.INVISIBLE);
                        break;
                    case 2:
                        tv_jinru.setVisibility(View.VISIBLE);
                        bt_tiaoguo.setVisibility(View.INVISIBLE);
                        break;
                    case 3:
                        tv_jinru.setVisibility(View.VISIBLE);
                        bt_tiaoguo.setVisibility(View.INVISIBLE);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        // 保存最新版信息
        PackageInfo mPackageInfo = DeviceUtil
                .getMPackageInfo(getApplicationContext());
        ShareUserInfoUtil.getInstance(context).setInt(ShareUserInfoUtil.SP_VERSION, mPackageInfo.versionCode);

    }

}
